package com.thingsgrid.device.monitor.service.impl;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import com.thingsgrid.device.entity.DeviceAlarm;
import com.thingsgrid.device.entity.Product;
import com.thingsgrid.device.monitor.service.AlarmMonitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AlarmMonitorServiceImpl implements AlarmMonitorService {

	@Autowired
	private MongoTemplate mongoTemplate;

	/**
	 *
	 *告警列表
	 * @author 陈学栋
	 * @date 2019/9/5 11:21
	 */
	@Override
	public Map<String, Object> findAlarmMonitorList(String startTime, String endTime,String alarmGrade,String deviceId, Integer current, Integer size) {
		HashMap<String, Object> map = new HashMap<>();
		Query query = new Query().skip((current - 1) * size).limit(size);
		if(startTime == null || startTime.equals("") && endTime == null || endTime.equals("") && alarmGrade ==null || alarmGrade.equals("") && deviceId == null || deviceId.equals("")){
			//列表
			List<DeviceAlarm> list = mongoTemplate.find(query, DeviceAlarm.class, "device_alarm");
			//总条数
			long pagecount = mongoTemplate.count(query, DeviceAlarm.class, "device_alarm");
			map.put("deviceAlarmList",list);
			map.put("total",pagecount);
		}else{
			query.addCriteria(Criteria.where("alarmCreateTime").gte(startTime+" 00:00:00").lte(endTime+" 00:00:00").and("alarmGrade").is(alarmGrade).and("deviceId").is(deviceId));
			List<DeviceAlarm> list = mongoTemplate.find(query, DeviceAlarm.class, "device_alarm");
			long pagecount = mongoTemplate.count(query, DeviceAlarm.class, "device_alarm");
			map.put("deviceAlarmList",list);
			map.put("total",pagecount);
		}
		return map;
	}

	/**
	 *
	 *告警统计
	 * @author 陈学栋
	 * @date 2019/9/5 11:22
	 */
	@Override
	public Map<String, String> alarmStatistics() {
		HashMap<String, String> map = new HashMap<>();

		//查询告警的所有数量
		long totalCount = mongoTemplate.count(new Query(), DeviceAlarm.class, "device_alarm");
		//查询告警的所有已解决的数量
		long totalResolvedCount = mongoTemplate.count(Query.query((Criteria.where("alarmStatus").is(1))), DeviceAlarm.class, "device_alarm");
		//查询告警级别为主要的数量
		long mainCount = mongoTemplate.count(Query.query((Criteria.where("alarmGrade").is("主要"))), DeviceAlarm.class, "device_alarm");

		//查询告警级别为主要的已解决的数量
		long mainResolvedCount = mongoTemplate.count(Query.query((Criteria.where("alarmGrade").is("主要").and("alarmStatus").is(1))), DeviceAlarm.class, "device_alarm");

		//查询告警级别为次要的数量
		long minorCount = mongoTemplate.count(Query.query(Criteria.where("alarmGrade").is("次要")), DeviceAlarm.class, "device_alarm");

		//查询告警级别为次要的已解决的数量
		long  minorResolvedCount = mongoTemplate.count(Query.query(Criteria.where("alarmGrade").is("次要").and("alarmStatus").is(1)), DeviceAlarm.class, "device_alarm");

		//查询告警级别为紧急的数量
		long exigencyCount = mongoTemplate.count(Query.query(Criteria.where("alarmGrade").is("紧急")), DeviceAlarm.class, "device_alarm");

		//查询告警级别为紧急的已解决的数量
		long  exigencyResolvedCount = mongoTemplate.count(Query.query(Criteria.where("alarmGrade").is("紧急").and("alarmStatus").is(1)), DeviceAlarm.class, "device_alarm");

		//查询告警级别为提示的数量
		long hintCount = mongoTemplate.count(Query.query(Criteria.where("alarmGrade").is("提示")), DeviceAlarm.class, "device_alarm");

		//查询告警级别为提示的已解决的数量
		long  hintResolvedCount = mongoTemplate.count(Query.query(Criteria.where("alarmGrade").is("提示").and("alarmStatus").is(1)), DeviceAlarm.class, "device_alarm");
		//告警的已解决统计
		map.put("totalStatistics",totalResolvedCount + "" + "/" + totalCount + "");
		//告警级别为主要的统计
		map.put("mainStatistics",mainResolvedCount + "" + "/" + mainCount + "");
		//告警级别为次要的统计
		map.put("minorStatistics",minorResolvedCount + "" + "/" + minorCount + "");
		//告警级别为紧急的统计
		map.put("exigencyStatistics",exigencyResolvedCount + "" + "/" + exigencyCount + "");
		//告警级别为提示的人统计
		map.put("hintStatistics",hintResolvedCount + "" + "/" +hintCount + "");
		return map;
	}

	/**
	 *
	 *删除告警
	 * @author 陈学栋
	 * @date 2019/9/5 13:11
	 */
	@Override
	public Boolean deleteAlarmMonitor(String alarmId) {
		DeleteResult deleteResult = mongoTemplate.remove(Query.query(Criteria.where("alarmId").is(alarmId)), DeviceAlarm.class, "device_alarm");
		if(deleteResult.getDeletedCount()==1){
			return true;
		}
		return false;
	}

	/**
	 *
	 *告警信息的确认
	 * @author 陈学栋
	 * @date 2019/9/5 13:27
	 */
	@Override
	public Boolean confirmAlarmMonitor(String alarmId) {

		List<DeviceAlarm> deviceAlarm = mongoTemplate.find(Query.query(Criteria.where("alarmId").is(alarmId)), DeviceAlarm.class, "device_alarm");
		DeviceAlarm alarm = deviceAlarm.get(0);
		Integer alarmStatus = alarm.getAlarmStatus();
		Query query = new Query();
		Criteria criteria = new Criteria();
		query.addCriteria(criteria.where("alarmId").is(alarm.getAlarmId()));
		Update update = new Update();
		if(alarmStatus == 0){
			update.set("alarmStatus", 1);
		}else{
			update.set("alarmStatus", 0);
		}
		UpdateResult updateResult = mongoTemplate.updateFirst(query, update, DeviceAlarm.class, "device_alarm");
		if(updateResult.getModifiedCount()==1){
			return true;
		}

		return false;
	}

	@Override
	public List<Product> findProductName() {
		return mongoTemplate.find(new Query(), Product.class, "product");
	}


}
